home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.01 Jan 90 / Jan Jorg Source / MacTutorApp.cp next >
Encoding:
Text File  |  1989-11-19  |  5.9 KB  |  264 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #    MacTutorApp
  3. #
  4. #    A rudimentary application skeleton
  5. #    J. Langowski / MacTutor 1989
  6. #------------------------------------------------------------------------------*/
  7. #include <Types.h>
  8. #include <QuickDraw.h>
  9. #include <Fonts.h>
  10. #include <Events.h>
  11. #include <OSEvents.h>
  12. #include <Controls.h>
  13. #include <Windows.h>
  14. #include <Menus.h>
  15. #include <TextEdit.h>
  16. #include <Dialogs.h>
  17. #include <Desk.h>
  18. #include <Scrap.h>
  19. #include <ToolUtils.h>
  20. #include <Memory.h>
  21. #include <SegLoad.h>
  22. #include <Files.h>
  23. #include <OSUtils.h>
  24. #include <Traps.h>
  25. #include <StdLib.h>
  26.  
  27. #include "TDocument.h"
  28. #include "TApplication.h"
  29. #include "MacTutorApp.h"
  30. #include "MacTutorDoc.h"
  31. #include "MacTutorGrow.h"
  32.  
  33. // Methods for our application class
  34. TMacTutorApp::TMacTutorApp(void)
  35. {
  36.     Handle    menuBar;
  37.  
  38.     // read menus into menu bar
  39.     menuBar = GetNewMBar(rMenuBar);
  40.     // install menus
  41.     SetMenuBar(menuBar);
  42.     DisposHandle(menuBar);
  43.     // add DA names to Apple menu
  44.     AddResMenu(GetMHandle(mApple), 'DRVR');
  45.     DrawMenuBar();
  46.  
  47.     // create empty mouse region
  48.     fMouseRgn = NewRgn();
  49.     // create a single empty document
  50.     DoNew();
  51. }
  52.  
  53. // Tell TApplication class how much heap we need
  54. long TMacTutorApp::HeapNeeded(void)
  55. {
  56.     return (kMinSize * 1024);
  57. }
  58.  
  59. // Calculate a sleep value for WaitNextEvent. This takes into account the things
  60. // that DoIdle does with idle time.
  61.  
  62. unsigned long TMacTutorApp::SleepVal(void)
  63. {
  64.     unsigned long sleep;
  65.     const long kSleepTime = 0x7fffffff;    // a very large positive number
  66.  
  67.     sleep = kSleepTime;                // default value for sleep
  68.     if ((!fInBackground))
  69.     {
  70.           sleep = GetCaretTime();    // A reasonable time interval for MenuClocks, etc.
  71.     }
  72.     return sleep;
  73. }
  74.  
  75. void TMacTutorApp::AdjustMenus(void)
  76. {
  77.     WindowPtr    frontmost;
  78.     MenuHandle    menu;
  79.     Boolean undo,cutCopyClear,paste;
  80.  
  81.     TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
  82.  
  83.     frontmost = FrontWindow();
  84.  
  85.     menu = GetMHandle(mFile);
  86.     if ( fDocList->NumDocs() < kMaxOpenDocuments )
  87.       EnableItem(menu, iNew);            // New is enabled when we can open more documents 
  88.     else DisableItem(menu, iNew);
  89.     if ( frontmost != (WindowPtr) nil )    // Close is enabled when there is a window to close 
  90.       EnableItem(menu, iClose);
  91.     else DisableItem(menu, iClose);
  92.  
  93.     undo = false;
  94.     cutCopyClear = false;
  95.     paste = false;
  96.     
  97.     if ( fMacTutorCurDoc == nil )
  98.       {
  99.         undo = true;                // all editing is enabled for DA windows 
  100.         cutCopyClear = true;
  101.         paste = true;
  102.       }
  103.       
  104.     menu = GetMHandle(mEdit);
  105.     if ( undo )
  106.         EnableItem(menu, iUndo);
  107.     else
  108.         DisableItem(menu, iUndo);
  109.     
  110.     if ( cutCopyClear )
  111.       {
  112.         EnableItem(menu, iCut);
  113.         EnableItem(menu, iCopy);
  114.         EnableItem(menu, iClear);
  115.       } 
  116.     else
  117.       {
  118.         DisableItem(menu, iCut);
  119.         DisableItem(menu, iCopy);
  120.         DisableItem(menu, iClear);
  121.       }
  122.       
  123.     if ( paste )
  124.         EnableItem(menu, iPaste);
  125.     else
  126.         DisableItem(menu, iPaste);
  127.         
  128.     menu = GetMHandle(myMenu);
  129.         EnableItem(menu, item1);
  130.         EnableItem(menu, item2);
  131.         EnableItem(menu, item3);
  132.         EnableItem(menu, item5);
  133.  
  134.         CheckItem(menu, item1, false);
  135.         CheckItem(menu, item2, false);
  136.         CheckItem(menu, item3, false);
  137.         CheckItem(menu, item5, false);
  138.         CheckItem(menu, fMacTutorCurDoc->GetItemSelected(), true);
  139. } // AdjustMenus
  140.  
  141.  
  142. void TMacTutorApp::DoMenuCommand(short menuID, short menuItem)
  143. {
  144.     short        itemHit;
  145.     Str255        daName;
  146.     short        daRefNum;
  147.     WindowPtr    window;
  148.     TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
  149.  
  150.     window = FrontWindow();
  151.     switch ( menuID )
  152.       {
  153.         case mApple:
  154.             switch ( menuItem )
  155.               {
  156.                 case iAbout:        // About box
  157.                     itemHit = Alert(rAboutAlert, nil);
  158.                     break;
  159.                 default:            // DAs etc.
  160.                     GetItem(GetMHandle(mApple), menuItem, daName);
  161.                     daRefNum = OpenDeskAcc(daName);
  162.                     break;
  163.               }
  164.             break;
  165.         case mFile:
  166.             switch ( menuItem )
  167.               {
  168.                 case iNew:
  169.                     DoNew();
  170.                     break;
  171.                 case iClose:
  172.                     if (fMacTutorCurDoc != nil)
  173.                       {
  174.                         fDocList->RemoveDoc(fMacTutorCurDoc);
  175.                         delete fMacTutorCurDoc;
  176.                       }
  177.                     else CloseDeskAcc(((WindowPeek) fWhichWindow)->windowKind);
  178.                     break;
  179.                 case iQuit:
  180.                     Terminate();
  181.                     break;
  182.               }
  183.             break;
  184.         case mEdit:                    // call SystemEdit for DA editing & MultiFinder 
  185.             if ( !SystemEdit(menuItem-1) )
  186.               {
  187.                 switch ( menuItem )
  188.                   {
  189.                     case iCut:
  190.                         break;
  191.                     case iCopy:
  192.                         break;
  193.                     case iPaste:
  194.                         break;
  195.                     case iClear:
  196.                         break;
  197.                    }
  198.               }
  199.             break;
  200.         case myMenu:
  201.             if (fMacTutorCurDoc != nil) 
  202.             {
  203.                 switch ( menuItem )
  204.                   {
  205.                     case item1:
  206.                         fMacTutorCurDoc->SetDisplayString("\pC++");
  207.                         break;
  208.                     case item2:
  209.                         fMacTutorCurDoc->SetDisplayString("\pSample");
  210.                         break;
  211.                     case item3:
  212.                         fMacTutorCurDoc->SetDisplayString("\pApplication");
  213.                         break;
  214.                     case item5:
  215.                         fMacTutorCurDoc->SetDisplayString("\pHave Fun");
  216.                         break;
  217.                    }
  218.             fMacTutorCurDoc->SetItemSelected(menuItem);
  219.             InvalRect(&(window->portRect));
  220.             fMacTutorCurDoc->DoUpdate();
  221.             }
  222.             break;
  223.       }
  224.     HiliteMenu(0);
  225. } // DoMenuCommand
  226.  
  227. // Create a new document and window. 
  228. void TMacTutorApp::DoNew(void)
  229. {
  230.     TMacTutorGrow* tMacTutorDoc;
  231.     
  232.     tMacTutorDoc = new TMacTutorGrow(rDocWindow,"\pNothing selected yet.");
  233.     // if we didn't get an allocation error, add it to list
  234.     if (tMacTutorDoc != nil)
  235.       fDocList->AddDoc(tMacTutorDoc);
  236. } // DoNew
  237.  
  238. void TMacTutorApp::Terminate(void)
  239. {
  240.     ExitLoop();
  241. } // Terminate
  242.  
  243. // Our application object, initialized in main(). We make it
  244. // global so our functions which don't belong to any class
  245. // can find the active document.
  246. TMacTutorApp *gTheApplication;
  247.  
  248. // main is the entrypoint to the program
  249. int main(void)
  250. {
  251.     // Create our application object. This MUST be the FIRST thing
  252.     // done in main(), since it initializes the Toolbox for us.
  253.     gTheApplication = new TMacTutorApp;
  254.     if (gTheApplication == nil)        // if we couldn't allocate object (impossible!?)
  255.       return 0;                        // go back to Finder
  256.     
  257.     // Start our main event loop running. This won't return until user quits
  258.     gTheApplication->EventLoop();
  259.  
  260.     // We always return a value, like good little ANSI worshippers
  261.     return 0;
  262. }
  263.  
  264.